package com.zhongke.cotrun.service.impl;


import com.zhongke.cotrun.mapper.LevelTwoCommentMapper;
import com.zhongke.cotrun.model.pojo.LevelTwoComment;
import com.zhongke.cotrun.model.pojo.Tags;
import com.zhongke.cotrun.service.LevelTwoCommentService;
import com.zhongke.cotrun.service.TagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.List;

@Service
public class LevelTwoCommentServiceImpl implements LevelTwoCommentService {

    @Autowired
    LevelTwoCommentMapper levelTwoCommentMapper;
    @Autowired
    TagsService tagsService;//点赞

    /**
     * 新建二级评论
     *
     * @param levelTwoComment
     * @return
     */
    @Override
    @Transactional
    public LevelTwoComment insertLevelTwoComment(LevelTwoComment levelTwoComment) {
        if (levelTwoCommentMapper.insertLevelTwoComment(levelTwoComment)) {
            Tags tags = new Tags();
            tags.setLinkCommentId(levelTwoComment.getLevelTwoCommentId());
            tags.setGarde(2);
            tags.setType(levelTwoComment.getType());
            if(tagsService.insertTags(tags)) {
                levelTwoComment.setTags(tags);
                return levelTwoComment;
            }
        }
        return null;
    }

    /**
     * 查询二级评论
     *
     * @param levelTwoComment
     * @return
     */
    @Override
    public List<LevelTwoComment> selectLevelTwoComment(LevelTwoComment levelTwoComment) {
        List<LevelTwoComment> l = levelTwoCommentMapper.selectLevelTwoComment(levelTwoComment);


        for (LevelTwoComment lt : l) {
            //查询二级的点赞数
            Tags tags = new Tags();
            tags.setLinkCommentId(lt.getLevelTwoCommentId());
            tags.setGarde(2);
            tags.setType(levelTwoComment.getType());
            lt.setTags(tagsService.selectTagesByLevelOneCommentId(tags));
            tags = null;
        }

        return l;
    }

    /**
     * 根据id查看二级评论
     * @param id
     * @return
     */
    @Override
    public LevelTwoComment selectLevelTwoCommentById(BigInteger id,String type) {

        LevelTwoComment levelTwoComment=new LevelTwoComment();
        levelTwoComment.setLevelTwoCommentId(id);
        levelTwoComment.setType(type);
        List<LevelTwoComment> l = levelTwoCommentMapper.selectLevelTwoComment(levelTwoComment);
        if(l==null||l.size()==0){
            return null;
        }

        for (LevelTwoComment lt : l) {
            //查询二级的点赞数
            Tags tags = new Tags();
            tags.setLinkCommentId(id);
            tags.setGarde(2);
            tags.setType(type);
            lt.setTags(tagsService.selectTagesByLevelOneCommentId(tags));
            tags = null;
        }

        return l.get(0);

    }

    /**
     * 更改二级评论的状态
     * @param state
     * @param levelTwoCommentId
     * @param userId
     * @return
     */
    @Override
    public Boolean updateStateLevelTwocomment(Integer state, Integer levelTwoCommentId, Integer userId) {
        Integer a = levelTwoCommentMapper.selectLevelTwoCommentLinkUserById(levelTwoCommentId);
        if (a != userId) {
            return false;
        }

        return  levelTwoCommentMapper.updateStateForLevelTwoComment(state,levelTwoCommentId);
    }

}
